package so.shanku.essential.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import net.tsz.afinal.json.JsonMap;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnItemClick;
import com.nostra13.universalimageloader.core.ImageLoader;
import so.shanku.essential.R;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.utils.ImageLoaderBitmapCallBack;
import so.shanku.essential.utils.StringUtil;
import so.shanku.essential.utils.Utils;
import so.shanku.essential.view.CircleImageView;
import striveen.util.used.view.view.HorizontalListView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Jaelyn on 16/3/3.
 * 商品晒单评论列表
 */
public class ProductCommentsAdapter extends MyBaseAdapter {


    /**
     * 按钮和推荐的列表的点击事件
     */
    private ItemClickListener itemClickListener;


    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }


    public ProductCommentsAdapter(Context context) {
        super(context);
    }

    public ProductCommentsAdapter(Context context, List<? extends Map<String, ?>> datas) {
        super(context, datas);
    }


    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        GoodsCommentsAdapterViewHolder commentsAdapterViewHolder;
        if (view == null) {
            commentsAdapterViewHolder = new GoodsCommentsAdapterViewHolder();
            view = layoutInflater.inflate(R.layout.goods_comments_item, null);
            ViewUtils.inject(commentsAdapterViewHolder, view);
            view.setTag(commentsAdapterViewHolder);
        } else {
            commentsAdapterViewHolder = (GoodsCommentsAdapterViewHolder) view.getTag();
        }
//        setTag(i, commentsAdapterViewHolder);
//        bindData(i, commentsAdapterViewHolder);
        return view;
    }

    private void setTag(int i, GoodsCommentsAdapterViewHolder commentsAdapterViewHolder) {
        commentsAdapterViewHolder.attach_pic_hv.setTag(i);
    }

    private void bindData(int i, GoodsCommentsAdapterViewHolder goodsCommentsAdapterViewHolder) {
        JsonMap map = (JsonMap) datas.get(i);
        goodsCommentsAdapterViewHolder.userName_tv.setText(map.getString("userName"));

        goodsCommentsAdapterViewHolder.create_time_tv.setText(StringUtil.getData_yyyy_MMBySeconds(map.getStringNoNull("createTime")));
        goodsCommentsAdapterViewHolder.goods_color_size_tv.setText(map.getStringNoNull("speStrName"));

        Utils.setUserIcon(map, goodsCommentsAdapterViewHolder.user_photo_iv);
        String commentsContent = map.getStringNoNull("commentsContent");

        goodsCommentsAdapterViewHolder.userComments_tv.setText(commentsContent);

        ArrayList<JsonMap<String, Object>> recommandGoodsList = (ArrayList<JsonMap<String, Object>>) map.getList_JsonMap("ProductList");
        if (recommandGoodsList.size() == 0) {
            goodsCommentsAdapterViewHolder.attach_pic_hv.setVisibility(View.GONE);
            RecommandProductAdapter recommandProductAdapter = new RecommandProductAdapter(context);
            recommandProductAdapter.setType(1);
            recommandProductAdapter.setDatas(recommandGoodsList);
            goodsCommentsAdapterViewHolder.attach_pic_hv.setAdapter(recommandProductAdapter);
        } else {
            goodsCommentsAdapterViewHolder.attach_pic_hv.setVisibility(View.VISIBLE);
        }
    }


    public class GoodsCommentsAdapterViewHolder {
        @ViewInject(R.id.user_photo_iv)
        public CircleImageView user_photo_iv;
        @ViewInject(R.id.userName_tv)
        public TextView userName_tv;
        @ViewInject(R.id.create_time_tv)
        public TextView create_time_tv;
        @ViewInject(R.id.goods_color_size_tv)
        public TextView goods_color_size_tv;
        @ViewInject(R.id.attach_pic_hv)
        public HorizontalListView attach_pic_hv;
        @ViewInject(R.id.userComments_tv)
        public TextView userComments_tv;

        @OnItemClick(R.id.attach_pic_hv)
        public void attach_pic_hv_ItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (itemClickListener != null) {
                //正式调接口的时候需要更具需要进行修改参数data
                itemClickListener.onItemClickListener(Constant.FindItemCompontType.RECOMMANDOODS, (Integer) attach_pic_hv.getTag(), position);
            }
        }
    }

    public interface ItemClickListener {
        /**
         * 返回item的空间的点击事件
         *
         * @param itemCompontType 点击事件的类型 RECOMMAND_ITEM_CLICK：推荐列表的点击事件  MORE_CLICK：更多点击
         * @param position        点击的item的位置
         * @param childPosition   当点击的是推荐列表的item 返回数据的子list的位置 否则返回-1
         */
        void onItemClickListener(Constant.FindItemCompontType itemCompontType, int position, int childPosition);
    }
}
